package cn.yunhe.util;

import org.apache.struts2.ServletActionContext;

import java.io.*;

public class UploadFileUtil {
    private File upload;
    private String uploadContentType;
    private String uploadFileName;

    private String realSavePath;
    private String finalSavePath;

    public void doUpload(File file,String finalSavePath) throws IOException {
        FileInputStream fis=new FileInputStream(file);
        byte[] buffer=new byte[1024];
        int length=-2;

        FileOutputStream fos=new FileOutputStream(finalSavePath);

        length=fis.read(buffer);
        while (length!=-1){
            fos.write(buffer);
            length=fis.read(buffer);
            System.out.println("<==length===>"+length);
        }

        fis.close();
        fos.close();
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getRealSavePath() {
        /*return realSavePath;*/
        return ServletActionContext.getServletContext().getRealPath(realSavePath);
    }

    public void setRealSavePath(String realSavePath) {
        this.realSavePath = realSavePath;
    }

    public String getFinalSavePath() {
        /*return finalSavePath;*/
        return getRealSavePath()+"\\"+getUploadFileName();
    }

    public void setFinalSavePath(String finalSavePath) {
        this.finalSavePath = finalSavePath;
    }
}
